package com.huadao.cart_ccs.experiment.progressOverview;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.ExperimentStageManager;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.common.adapter.CommonRecycleAdapter;
import com.huadao.cart_ccs.common.adapter.CommonViewHolder;
import com.huadao.cart_ccs.common.dialog.DialogFragmentHelper;
import com.huadao.cart_ccs.common.dialog.IDialogResultListener;
import com.huadao.cart_ccs.data.bo.ExperimentStage;
import com.huadao.cart_ccs.greendao.entity.Operation;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ProgressOverViewDF extends BaseDialogFragment implements IProgressOverViewContract.IProgressOverViewView {

    @BindView(R.id.rv_stage)
    RecyclerView rv_stage;
    @BindView(R.id.rv_operate)
    RecyclerView rv_operate;
    @BindView(R.id.btn_changeName)
    Button btn_changeName;

    private ProgressOverViewPresenter progressOverViewPresenter;

    private List<ExperimentStage> experimentStageList;
    private List<Operation> operationList;
    private StageAdapter stageAdapter;
    private OperateAdapter operateAdapter;

    public interface OnStageNameChange{
        void onStageNameChange(String newName);
    }
    private OnStageNameChange onStageNameChange;

    @Override
    protected BasePresenter getPresenter() {
        return progressOverViewPresenter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_progress_overview;
    }

    @Override
    protected void onInit() {
        progressOverViewPresenter=new ProgressOverViewPresenter(this);
        experimentStageList=new ArrayList<>();
        initStageList();
        operationList=new ArrayList<>();
        initOperateList();
        btn_changeName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeName();
            }
        });
    }

    private void changeName() {
        DialogFragmentHelper.showNormalInputDialog(getActivity(), "设置新名字", null,ExperimentStageManager.getInstance().getCurrentStage().getStage(), new IDialogResultListener<Object[]>() {
            @Override
            public void onDataResult(Object[] result) {
                int res= (int) result[0];
                if(res==DialogInterface.BUTTON_POSITIVE){
                    String newName= (String) result[1];
                    if(TextUtils.isEmpty(newName)){
                        showToast("新名字不能为空");
                        return;
                    }else {
                        progressOverViewPresenter.setNewName(newName);
                    }
                }
            }
        });
    }

    private void initStageList() {
        stageAdapter=new StageAdapter();
        rv_stage.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_stage.setAdapter(stageAdapter);
        progressOverViewPresenter.getHappenedStageList();
    }

    private void initOperateList() {
        operateAdapter=new OperateAdapter();
        rv_operate.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_operate.setAdapter(operateAdapter);
        progressOverViewPresenter.getOperateList();
    }

    @Override
    public ExperimentStage getCurrentStage() {
        return ExperimentStageManager.getInstance().getCurrentStage();
    }

    @Override
    public void onGetHappenedStageList(List<ExperimentStage> experimentStageList) {
        this.experimentStageList.addAll(experimentStageList);
        stageAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetOperateList(List<Operation> operationList) {
        this.operationList.addAll(operationList);
        operateAdapter.notifyDataSetChanged();
    }

    @Override
    public void setNewNameSucc(String newName) {
        stageAdapter.notifyItemChanged(experimentStageList.size()-1);
        experimentStageList.get(experimentStageList.size()-1).setStage(newName);
        showToast("设置名字成功");
        if(onStageNameChange!=null){
            onStageNameChange.onStageNameChange(newName);
        }
    }

    public void setStageNameChange(OnStageNameChange onStageNameChange) {
        this.onStageNameChange = onStageNameChange;
    }

    @Override
    public void setNewNameFailed() {
        showToast("设置名字失败");
    }

    class StageAdapter extends RecyclerView.Adapter{

        private int TYPE_before=0;
        private int TYPE_current=1;

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
            if(viewType==TYPE_before){
                View view=layoutInflater.inflate(R.layout.item_stage,parent,false);
                return new StageHolder(view);
            }else if(viewType==TYPE_current){
                View view=layoutInflater.inflate(R.layout.item_stage_current,parent,false);
                return new CurrentStageHolder(view);
            }
            return null;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ExperimentStage stage=experimentStageList.get(position);
            if(holder instanceof StageHolder){
                ((StageHolder) holder).setText(R.id.tv_stage,String.format("Day%d %s",stage.getDay(),stage.getStage()));
            }else if(holder instanceof CurrentStageHolder){
                ((CurrentStageHolder) holder).setText(R.id.tv_stage_day,String.format("Day%d",stage.getDay()));
                ((CurrentStageHolder) holder).setText(R.id.tv_stage_progress,String.format("%s",stage.getStage()));
            }
        }

        @Override
        public int getItemCount() {
            return experimentStageList.size();
        }

        @Override
        public int getItemViewType(int position) {
            if(position<experimentStageList.size()-1){
                return TYPE_before;
            }else {
                return TYPE_current;
            }
        }

        class StageHolder extends CommonViewHolder{

            public StageHolder(View itemView) {
                super(itemView);
            }
        }

        class CurrentStageHolder extends CommonViewHolder{

            public CurrentStageHolder(View itemView) {
                super(itemView);
            }
        }
    }

    class OperateAdapter extends RecyclerView.Adapter{

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
            View view=layoutInflater.inflate(R.layout.item_operate,parent,false);
            return new OperateHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            OperateHolder operateHolder= (OperateHolder) holder;
            Operation operation=operationList.get(position);
            operateHolder.setText(R.id.tv_operate,String.format("%s-%s-%s",operation.getOperator(),operation.getName(),operation.getData()));
            operateHolder.setText(R.id.tv_date,TimeUtil.log_sdf.format(operation.getDateTime()));
        }

        @Override
        public int getItemCount() {
            return operationList.size();
        }

        class OperateHolder extends CommonViewHolder{

            public OperateHolder(View itemView) {
                super(itemView);
            }
        }
    }
}
